package com.helloandroid.activesitting.ui;

import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

import com.helloandroid.activesitting.R;
import com.helloandroid.activesitting.bc.PreferencesConnector;
import com.helloandroid.activesitting.bl.NotificationActiveSittingManager;
import com.helloandroid.activesitting.bl.TimeManager;
import com.helloandroid.activesitting.bo.WorkTime;

public class MainActivity2 extends PreferenceActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.mainscreen2);
		
		TimePreference startWork = (TimePreference) findPreference("work_start_time");
		startWork.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				PreferencesConnector.GetInstance().SetWorkTimeByKey((WorkTime) newValue, PreferencesConnector.KEY_TIME_START_HOUR, PreferencesConnector.KEY_TIME_START_MIN);
				return true;
			}
		});
		
		TimePreference endWork = (TimePreference) findPreference("work_end_time");
		endWork.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				PreferencesConnector.GetInstance().SetWorkTimeByKey((WorkTime) newValue, PreferencesConnector.KEY_TIME_END_HOUR, PreferencesConnector.KEY_TIME_END_MIN);
				return true;
			}
		});
		TimePreference startLunch = (TimePreference) findPreference("lunch_start_time");
		startLunch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				PreferencesConnector.GetInstance().SetWorkTimeByKey((WorkTime) newValue, PreferencesConnector.KEY_LUNCH_END_MINUTES, PreferencesConnector.KEY_LUNCH_START_MINUTES);
				return true;
			}
		});
		TimePreference endLunch = (TimePreference) findPreference("lunch_end_time");
		endLunch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				PreferencesConnector.GetInstance().SetWorkTimeByKey((WorkTime) newValue, PreferencesConnector.KEY_LUNCH_END_HOURS, PreferencesConnector.KEY_LUNCH_END_MINUTES);
				return true;
			}
		});
		
		CheckBoxPreference enabledLunch = (CheckBoxPreference) findPreference("KEY_LUNCH_ENABLED");
		enabledLunch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				
				PreferencesConnector.GetInstance().SetBooleanByKey(PreferencesConnector.KEY_LUNCH_ENABLED, Boolean.parseBoolean(newValue.toString()));
				return true;
			}
		});
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		TimeManager time = new TimeManager();
		time.StartTimers();
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm");
		NotificationActiveSittingManager.ShowOnGoingNotification(format.format(time.getLastSetUpAlarmTime()));
	}
	

	

}
